#include "Phaser.h"
#include <math.h>

GLfloat phaser_diffuse[] = {0.7, 0.7, 0.7, 1.0};
GLfloat phaser_ambient[] = {0.7, 0.7, 0.7, 1.0};
GLfloat phaser_specular[] = {0.5, 0.5, 0.5, 1.0};

// Shininess
GLfloat phaser_shininess = 10.0f;

Phaser::Phaser(void)
{
	heightCubeBaseP = 0.1;
	widthP = 1.0; 
	radiusTorusP = 0.25;
	quad = gluNewQuadric();
	loadImage();
}

void Phaser::loadImage()
{
	Image* image1 = loadBMP("textures\\textureBipod.bmp");
	Image* image2 = loadBMP("textures\\textureBipod2.bmp");
	Image* image3 = loadBMP("textures\\alu.bmp");
	Image* image4 = loadBMP("textures\\copper.bmp");
	Image* image5 = loadBMP("textures\\metal.bmp");
	Image* image6 = loadBMP("textures\\textureRusty.bmp");
	textureBase1 = loadTexture(image1);
	textureBase2 = loadTexture(image2);
	textureCylindre1 = loadTexture(image3);
	textureCylindre2 = loadTexture(image4);
	texturePointe1 = loadTexture(image5);
	texturePointe2 = loadTexture(image6);

	currentTextureBase = textureBase1;
	currentTextureCylindre = textureCylindre1;
	currentTexturePointe = texturePointe1;
	
	delete image1;
	delete image2;
	delete image3;
	delete image4;
	delete image5;
	delete image6;
}

void Phaser::switchTexture()
{
	if(currentTextureBase == textureBase1)
		currentTextureBase = textureBase2;
	else
		currentTextureBase = textureBase1;
	if(currentTextureCylindre == textureCylindre1)
		currentTextureCylindre = textureCylindre2;
	else
		currentTextureCylindre = textureCylindre1;
	if(currentTexturePointe == texturePointe1)
		currentTexturePointe = texturePointe2;
	else
		currentTexturePointe = texturePointe1;
}

void Phaser::display()
{
	glPushMatrix();
		glTranslatef(0.0, elevation + heightCubeBaseP/2, 0.0);

		glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, phaser_diffuse);
		glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, phaser_ambient );
		glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, phaser_specular);
		glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, phaser_shininess);

		glColor3f(1,1,1);

		// top and base
		glPushMatrix();
			glBindTexture(GL_TEXTURE_2D, currentTextureBase);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
			// Cube Base
			glPushMatrix();
				glScalef(1.0, heightCubeBaseP, 1.0);
				drawCube(0.5,0.55);
			glPopMatrix();
			// Cube Top
			glPushMatrix();
				glTranslatef(0.0, radiusTorusP*2+heightCubeBaseP, 0.0);
				glScalef(1.0, heightCubeBaseP, 1.0);
				drawCube(0.5,0.55);
			glPopMatrix();
		glPopMatrix();

		gluQuadricNormals(quad, GLU_SMOOTH);   // Create Smooth Normals (better for light)
		gluQuadricTexture(quad, GL_TRUE);      // Create Texture Coords

		glBindTexture(GL_TEXTURE_2D, currentTextureCylindre);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
		//Right Gun
		glPushMatrix();
			glTranslatef(0.5 - radiusTorusP, heightCubeBaseP/2 + radiusTorusP,-0.5);
			glPushMatrix();
				gluCylinder(quad, radiusTorusP, radiusTorusP, 1.0, 30.0, 3.0);
			glPopMatrix();
			//back disk
			glPushMatrix();
				glRotatef(180, 1, 0,0);
				gluDisk(quad, 0, radiusTorusP, 30.0, 3.0);
			glPopMatrix();
			//front disk
			glPushMatrix();
				glTranslatef(0,0, 1.0);
				gluDisk(quad, 0, radiusTorusP, 30.0, 3.0);
			glPopMatrix();
			// Tube
			glPushMatrix();
				glTranslatef(0,0,1);
				glScalef(0.15, 0.15, 0.2);
				createTube();
			glPopMatrix();
			// Cube
			glPushMatrix();
				glTranslatef(0, 0, 1.2);
				glScalef(0.15, 0.15, 0.05);
				drawCube();
			glPopMatrix();

			glBindTexture(GL_TEXTURE_2D, currentTexturePointe);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
			//Pointer
			glPushMatrix();
				glTranslatef(0, 0, 1.205);
				gluCylinder(quad, 0.05, 0, 0.4, 30.0, 3.0);
			glPopMatrix();
		glPopMatrix();
		
		glBindTexture(GL_TEXTURE_2D, currentTextureCylindre);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
		//Left Gun
		glPushMatrix();
			glTranslatef(radiusTorusP - 0.5, heightCubeBaseP/2 + radiusTorusP,-0.5);
			glPushMatrix();
				gluCylinder(quad, radiusTorusP, radiusTorusP, 1.0, 30.0, 3.0);
			glPopMatrix();
			//back disk
			glPushMatrix();
				glRotatef(180, 1, 0,0);
				gluDisk(quad, 0, radiusTorusP, 30.0, 3.0);
			glPopMatrix();
			//front disk
			glPushMatrix();
				glTranslatef(0,0, 1.0);
				gluDisk(quad, 0, radiusTorusP, 30.0, 3.0);
			glPopMatrix();
			// Tube
			glPushMatrix();
				glTranslatef(0,0,1);
				glScalef(0.15, 0.15, 0.2);
				createTube();
			glPopMatrix();
			// Cube
			glPushMatrix();
				glTranslatef(0, 0, 1.2);
				glScalef(0.15, 0.15, 0.05);
				drawCube();
			glPopMatrix();

			glBindTexture(GL_TEXTURE_2D, currentTexturePointe);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
			//Pointer
			glPushMatrix();
				glTranslatef(0, 0, 1.205);
				gluCylinder(quad, 0.05, 0, 0.4, 30.0, 3.0);
			glPopMatrix();
		glPopMatrix();
	glPopMatrix();
}

void Phaser::createTube()
{	
	glPushMatrix();
		gluCylinder(quad, 1.0, 1.0, 1.0, 30.0, 3.0);
	glPopMatrix();
	// Caps
	glPushMatrix();
		glRotatef(180, 1, 0,0);
		gluDisk(quad, 0.0, 1.0, 30.0, 3.0);
	glPopMatrix();

	glPushMatrix();
		glTranslatef(0.0, 0.0, 1.0);
		gluDisk(quad, 0.0, 1.0, 30.0, 3.0);
	glPopMatrix();
}

float Phaser::getHeight()
{
	return radiusTorusP*2 + heightCubeBaseP*2;
}

Phaser::~Phaser(void)
{
	gluDeleteQuadric(quad);
}
